package com.zdglf.mmd.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zdglf.mmd.R;
import com.zdglf.mmd.adapter.FileListAdapter;
import com.zdglf.mmd.utils.FileUtils;

import java.io.File;
import java.util.List;

public class FileListActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private static final String FILE_FILTER = ".pmd|.pmx|.obj";
    ListView lv_file_list ;
    FileListAdapter fileListAdapter;
    TextView tv_path;
    File currentFile;
    String sdCardPath;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_list);
        initView();
        initListener();
        initData();
    }

    private void initData() {
        fileListAdapter = new FileListAdapter();
        lv_file_list.setAdapter(fileListAdapter);
        currentFile = Environment.getExternalStorageDirectory();
        sdCardPath = currentFile.getAbsolutePath();
        reloadDirectory(currentFile);
    }

    private void initListener() {
        lv_file_list.setOnItemClickListener(this);
    }

    private void initView() {
        lv_file_list = (ListView)findViewById(R.id.lv_file_list);
        tv_path = (TextView)findViewById(R.id.tv_path);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        File f = (File)fileListAdapter.getItem(position);
        if(f.isDirectory()){
            reloadDirectory(f);
        }else{
            Intent it = new Intent();
            it.setData(Uri.fromFile(f));
            if(f.getName().endsWith("pmx")){

                it.setClass(this, GoPMXModelActivity.class);
                startActivity(it);
            }else if(f.getName().endsWith("pmd")){
                it.setClass(this, GoPMDModelActivity.class);
                startActivity(it);
            }else if(f.getName().endsWith("obj")){
                it.setClass(this, GoObjModelActivity.class);
                startActivity(it);
            }else{
                Toast.makeText(this, R.string.not_support_format, Toast.LENGTH_LONG).show();
            }

        }
    }

    public void reloadDirectory(File f){
        currentFile = f;
        tv_path.setText(currentFile.getAbsolutePath().toString());
        List<File> fileList = FileUtils.getDirectoryFileList(currentFile, FILE_FILTER);
        fileListAdapter.clear();
        fileListAdapter.addAll(fileList);
        fileListAdapter.notifyDataSetChanged();

    }

    @Override
    public void onBackPressed() {
        if(sdCardPath.equals(currentFile.getAbsolutePath().toString())){
            super.onBackPressed();
        }else{
            reloadDirectory(currentFile.getParentFile());
        }

    }
}
